package com.example.base.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.contract.ActivityResultContracts;

import com.example.base.R;
import com.example.base.Utils.CookieJarManager;
import com.example.base.activity.LoginActivity;
import com.example.base.bean.HomeThree;
import com.example.base.bean.Object;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class MyAdapter2 extends BaseAdapter {
  public List<HomeThree> getDataList() {
    return dataList;
  }

  private List<HomeThree> dataList = new ArrayList<>();
  private Context context;


  public MyAdapter2(Context context, List<HomeThree> dataList) {
    this.dataList.addAll(dataList);
    this.context = context;
  }

  @Override
  public int getCount() {
    return dataList.size();
  }

  @Override
  public Object getItem(int position) {
    return null;
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    MyViewHolder myViewHolder;
    if (convertView == null) {
      myViewHolder = new MyViewHolder();
      LayoutInflater inflater = LayoutInflater.from(context);
      convertView = inflater.inflate(R.layout.list_view, null, true);
      myViewHolder.tv_from = convertView.findViewById(R.id.textview1);
      myViewHolder.tv_time = convertView.findViewById(R.id.textview2);
      myViewHolder.tv_title = convertView.findViewById(R.id.textview3);
      myViewHolder.tv_name = convertView.findViewById(R.id.textview4);
      myViewHolder.imageView = convertView.findViewById(R.id.imagview1);
      convertView.setTag(myViewHolder);
    } else {
      myViewHolder = (MyViewHolder) convertView.getTag();
    }
    HomeThree homeThree = dataList.get(position);
    myViewHolder.tv_from.setText(homeThree.getShareUser());
    myViewHolder.tv_time.setText(homeThree.getNiceDate());
    myViewHolder.tv_title.setText(homeThree.getTitle());
    myViewHolder.tv_name.setText(homeThree.getSuperChapterName());
    myViewHolder.imageView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        String urileft = "https://www.wanandroid.com/lg/uncollect_originId/";
        int id;
        String uriRight = "/json";
        id = homeThree.getOriginId();
        SharedPreferences sp = v.getContext().getSharedPreferences("cookies", Context.MODE_PRIVATE);
        String value1 = sp.getString("loginUserName_wanandroid_com", "null");
        String value2 = sp.getString("loginUserName", "null");
        String value3 = sp.getString("token_pass_wanandroid_com", "null");
        String value4 = sp.getString("token_pass", "null");
        OkHttpClient.Builder client = new OkHttpClient.Builder();
        RequestBody body = new FormBody.Builder()
            .add("originId", String.valueOf(id))
            .build();
        Request request = new Request.Builder()
            .url(urileft + id + uriRight)
            .post(body)
            .addHeader("cookie", value1)
            .addHeader("cookie", value2)
            .addHeader("cookie", value3)
            .addHeader("cookie", value4)
            .build();
        Call call = client.build().newCall(request);
        call.enqueue(new Callback() {
          @Override
          public void onFailure(@NotNull Call call, @NotNull IOException e) {
            Log.i("myadapter", "onFailure: ");
          }

          @Override
          public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
            Log.i("myadapter", "onResponse: ");
          }
        });
        Toast.makeText(context, "取消收藏成功", Toast.LENGTH_SHORT).show();
      }

    });
    return convertView;
  }


  public void setData(List<HomeThree> data) {
    dataList.addAll(data);
  }


  class MyViewHolder {
    TextView tv_from;
    TextView tv_time;
    TextView tv_title;
    TextView tv_name;
    ImageView imageView;
  }
}